home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 271_02 / gtod.c < prev    next >
Text File  |  1987-10-14  |  7KB  |  234 lines

  1. /*
  2. **                GET TIME OF DAY UTILITY
  3. **
  4. ** S.E. Margison  Copyright 1987  All rights reserved
  5. ** 8-12-87 A  V1.02  Turbo-C
  6. **
  7. **  Options default to date, time, 12 hour format, no seconds, American
  8. **          style reporting.  Either - or / used for options, or none.
  9. **  -d   report (D)ate only
  10. **  -t   report (T)ime only
  11. **  -s   report (S)econds (only if -t option used)
  12. **  -l   Report date in (L)ong format
  13. **  -n   Include the (N)ame of the weekday, if -l format
  14. **  -m   report time in (M)ilitary format (12 hour format if not used)
  15. **  -e   report date in (E)uropean format
  16. **  -r   for time and date reporting, (R)everse order to time first
  17. **  If any options are used, no default values are assumed.
  18. **  No options is the same as -dt
  19. **
  20. **   As distributed, this program requires (for compilation):
  21. **     "Steve's Turbo-C Library" version 1.30 or later
  22. **   which may be obtained without registration from many Bulletin
  23. **   Board Systems including:
  24. **      Compuserve IBMSW
  25. **      Cul-De-Sac (Holliston, MA.)
  26. **      GEnie
  27. **   and software library houses including:
  28. **      Public (Software) Library (Houston, TX.)
  29. **
  30. **   or by registration:
  31. **      $10 for Docs, Small Model Library
  32. **      $25 for Docs, C, S, M, L, H libraries, and complete library source
  33. **              in C and Assembler
  34. **     Steven E. Margison
  35. **     124 Sixth Street
  36. **     Downers Grove, IL, 60515
  37. **
  38. */
  39.  
  40. #include <stdio.h>
  41. #include <time.h>
  42. #include <smdefs.h>
  43.  
  44. int nf, tf, df, sf, mf, rf, ef, lf;   /* option flags */
  45.  
  46. char datestr[64],   /* place for formatted date string */
  47.      timestr[16];   /* place for formatted time sring */
  48.  
  49. extern char *wkdayname();
  50. extern char *monthis();
  51.  
  52. struct tm *ptm;
  53. long clockval;
  54.  
  55. main(argc, argv)
  56. int argc;
  57. char *argv[];
  58. {
  59.  
  60.     /* first thing to do is read the clock! */
  61.  
  62.    time(&clockval);
  63.    ptm = localtime(&clockval);
  64.    doargs(argc, argv);  /* parse any options */
  65.  
  66.    if(!tf and !df) tf = df = YES;
  67.  
  68.    if(!tf) timestr[0] = NULL;   /* skip the time */
  69.    else mktime();
  70.  
  71.    if(!df) datestr[0] = NULL;   /* skip the date */
  72.    else mkdate();
  73.  
  74.    if(!rf) printf("%s  %s\n", datestr, timestr);
  75.    else printf("%s  %s\n", timestr, datestr);
  76. }
  77.  
  78. doargs(argc, argv)
  79. int argc;
  80. char *argv[];
  81. {
  82.    int i;
  83.    nf = tf = df = sf = mf = rf = ef = lf = NO;
  84.    while(--argc > 0) {
  85.       i = 0;
  86.       while(argv[argc][i] isnot NULL) {
  87.          switch(tolower(argv[argc][i++])) {
  88.             case '-': case '/':
  89.                break;
  90.             case 'n':
  91.                nf = YES;
  92.                break;
  93.             case 'd':
  94.                df = YES;
  95.                break;
  96.             case 't':
  97.                tf = YES;
  98.                break;
  99.             case 's':
  100.                sf = YES;
  101.                break;
  102.             case 'm':
  103.                mf = YES;
  104.                break;
  105.             case 'r':
  106.                rf = YES;
  107.                break;
  108.             case 'e':
  109.                ef = YES;
  110.                break;
  111.             case 'l':
  112.                lf = YES;
  113.                break;
  114.             case 'v':
  115.                fputs("GTOD Version 1.04\n", stderr);
  116.                fputs("GTOD ? for usage message\n", stderr);
  117.                error("Copyright 1987 S.E. Margison");
  118.             case '?':
  119.                usage();
  120.             default:
  121.                fputc(argv[argc][--i], stderr);
  122.                error(": invalid option");
  123.             }  /* end of switch */
  124.          } /* end of inner while */
  125.       }  /* end of outer while */
  126.    }
  127.  
  128. mkdate()
  129. {
  130.    int i, mpos, dpos;
  131.    char *mstr, dstr[6];
  132.    mpos = 0;
  133.    dpos = 3;
  134.    if(lf) {
  135.       dstr[0] = (ptm->tm_mday / 10) | '0';
  136.       if(dstr[0] is '0') dstr[0] = ' ';
  137.       dstr[1] = (ptm->tm_mday % 10) | '0';
  138.       dstr[2] = NULL;
  139.       if(nf) {
  140.          i = weekday(ptm->tm_mon+1, ptm->tm_mday, ptm->tm_year+1900);
  141.          mstr = wkdayname(i);
  142.          strcpy(datestr, mstr);
  143.          strcat(datestr, ", ");
  144.          }
  145. /* use the following line if you have the "fixed" monthis() function */
  146. /*      mstr = monthis(ptm->tm_mon);  */
  147. /* use the following line if you have the "bad" monthis() function */
  148.       mstr = monthis(ptm->tm_mon+1);
  149.  
  150.       if(ef) {
  151.          strcat(datestr, dstr);
  152.          strcat(datestr, " ");
  153.          strcat(datestr, mstr);
  154.          }
  155.       else {
  156.          strcat(datestr, mstr);
  157.          strcat(datestr, " ");
  158.          strcat(datestr, dstr);
  159.          }
  160.       strcat(datestr, ", ");
  161.       i_dstr(dstr, ptm->tm_year+1900);
  162.       strcat(datestr, dstr);
  163.       return;
  164.       }
  165.  
  166.    if(ef) { mpos = 3; dpos = 0; }
  167.  
  168.    ptm->tm_mon++;    /* insure it is in range 1-12 */
  169.    datestr[mpos++] = (ptm->tm_mon / 10) | '0';
  170.    datestr[mpos] = (ptm->tm_mon % 10) | '0';
  171.    datestr[2] = datestr[5] = '/';
  172.    datestr[dpos++] = (ptm->tm_mday / 10) | '0';
  173.    datestr[dpos] = (ptm->tm_mday % 10) | '0';
  174.    i = ptm->tm_year;
  175.    if(i > 100) i -= 100;
  176.    datestr[6] = (i / 10) | '0';
  177.    datestr[7] = (i % 10) | '0';
  178.    datestr[8] = NULL;
  179.    }
  180.  
  181. mktime()
  182. {
  183.    int hrs, eos;
  184.    timestr[2] = ':';
  185.    timestr[3] = (ptm->tm_min / 10) | '0';
  186.    timestr[4] = (ptm->tm_min % 10) | '0';
  187.    if(sf) {
  188.       timestr[5] = ':';
  189.       timestr[6] = (ptm->tm_sec / 10) | '0';
  190.       timestr[7] = (ptm->tm_sec % 10) | '0';
  191.       eos = 8;
  192.       }
  193.    else eos = 5;
  194.    if(mf) {
  195.       timestr[0] = (ptm->tm_hour / 10) | '0';
  196.       timestr[1] = (ptm->tm_hour % 10) | '0';
  197.       timestr[eos] = NULL;
  198.       }
  199.    else {
  200.       timestr[eos++] = ' ';
  201.       timestr[eos] = 'A';
  202.  
  203.       if(ptm->tm_hour >= 12) timestr[eos] = 'P';  /* set PM */
  204.  
  205.       if(ptm->tm_hour > 12) hrs = ptm->tm_hour - 12;
  206.       else if(ptm->tm_hour is 0) hrs = 12;   /* midnight = 12AM */
  207.       else hrs = ptm->tm_hour;
  208.  
  209.       timestr[++eos] = 'M';
  210.       timestr[++eos] = NULL;
  211.       timestr[0] = (hrs / 10) | '0';
  212.       timestr[1] = (hrs % 10) | '0';
  213.       if(timestr[0] == '0') timestr[0] = ' ';
  214.       }
  215.    }
  216.  
  217. usage() {
  218. puts("GTOD <options> or <-options> or </options>");
  219. puts("redirect stdout as desired");
  220. puts("d   report (D)ate only");
  221. puts("t   report (T)ime only");
  222. puts("s   report (S)econds (only if time is enabled)");
  223. puts("l   Report date in (L)ong format  (September 10, 1986)");
  224. puts("n   Include the (N)ame of the weekday, if -l format");
  225. puts("m   report time in (M)ilitary format (else, 12 hour format with AM/PM)");
  226. puts("e   report date in (E)uropean format  (DD/MM/YY)");
  227. puts("r   for time and date reporting, (R)everse order to time first");
  228. puts("    No options is the same as -dt:  MM/DD/YY hh:mm");
  229. puts("v   Version number and copyright message");
  230. puts("?   This help message");
  231. aabort(BELL);
  232.    }
  233.  
  234.